

import 'dart:convert';

import 'package:bt_tool/basic/bt_common_define.dart';
import 'package:bt_tool/basic/bt_controller.dart';
import 'package:bt_tool/widget/bt_base_btn_widget.dart';
import 'package:bt_tool/widget/bt_check_box.dart';
import 'package:bt_tool/widget/bt_text_field.dart';
import 'package:bt_tool/widget/bt_widget.dart';
import 'package:crypto/crypto.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo/common/config/app_theme_color_config.dart';
import 'package:flutter_demo/tools/encrypt/encrypt_main_controller.dart';
import 'package:get/get.dart';


class EncryptMainPage extends BTStatefulWidget{

  const EncryptMainPage({super.key,super.routerConfig});

  @override
  State<StatefulWidget> createState() {
    return EncryptMainPageState();
  }

}

class EncryptMainPageState extends BTState<EncryptMainPage>{

  final EncryptMainController ctl = EncryptMainController();

  @override
  BTController btCtrl() {
    return ctl;
  }



  @override
  Widget build(BuildContext context) {
    return Container(
      constraints: const BoxConstraints.expand(),
      padding: const EdgeInsets.all(10),
      color: AppThemeColor.contentBgColor,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Text("转换内容"),
          const SizedBox(height: 10,),
          Expanded(child: Container(
            constraints: const BoxConstraints.expand(),
            child: BTTextFieldWidget(
              btTextFieldController: ctl.inputCtl,
              maxLine: BTTextFieldWidget.maxLineDefine,
              isIgnoreClearBtn: true,
            ),
          ),),
          const SizedBox(height: 10,),
          const Text("转换结果"),
          const SizedBox(height: 10,),
          Expanded(child: Container(
            constraints: const BoxConstraints.expand(),
            child: BTTextFieldWidget(
              btTextFieldController: ctl.resultCtl,
              maxLine: BTTextFieldWidget.maxLineDefine,
              isIgnoreClearBtn: true,
            ),
          ),),
          const SizedBox(height: 15,),
          Obx(()=>BTCheckBoxGroup(
            titleArray: const ["加密","解密"],
            clickCallback: (index){
              ctl.actionType.value = EncryptAction.fromInt(index);
            },
            selectIndex: ctl.actionType.value.iValue,
            selectImg: Image.asset(btPngPath("bt/bt_check_box_sel")),
            normalImg: Image.asset(btPngPath("bt/bt_check_box_nor")),
          )),
          const SizedBox(height: 15,),

          Obx(()=> BTCheckBoxGroup(
            titleArray: const ["base64","md5","des3"],
            clickCallback: (i){
              ctl.cryptType.value = EncryptType.fromInt(i);
            },
            selectIndex: ctl.cryptType.value.iValue,
            selectImg: Image.asset(btPngPath("bt/bt_check_box_sel")),
            normalImg: Image.asset(btPngPath("bt/bt_check_box_nor")),
          )),

          Obx((){
            if(ctl.cryptType.value == EncryptType.des3){
              return Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  const SizedBox(height: 15,),
                  Row(
                    children: [
                      const Text("秘钥:"),
                      const SizedBox(width: 10,),
                      Container(
                        constraints: const BoxConstraints.expand(width: 200,height: 35),
                        child: BTTextFieldWidget(
                          btTextFieldController: ctl.des3KeyCtl,
                          bgCornerRadius: 4,
                          textFieldPadding: const EdgeInsets.fromLTRB(12, 2, 0, 0),

                        ),

                      ),
                      const SizedBox(width: 20,),
                      const Text("iv:"),
                      const SizedBox(width: 10,),
                      Container(
                        constraints: const BoxConstraints.expand(width: 200,height: 35),
                        child: BTTextFieldWidget(
                          btTextFieldController: ctl.des3ivCtl,
                          bgCornerRadius: 4,
                          textFieldPadding: const EdgeInsets.fromLTRB(12, 2, 0, 0),

                        ),

                      ),
                    ],
                  )
                ],
              );
            }
            return Container();
          }),

          const SizedBox(height: 15,),
          Row(
            children: [
              BTBtnTextWidget(
                bgColor: AppThemeColor.mainColor,
                constraints: const BoxConstraints.expand(width: 100,height: 40),
                text: "操作",
                textColor: Colors.white,
                onTap: () => ctl.action(),
                alignment: Alignment.center,
              ),
              const SizedBox(width: 10,),
              BTBtnTextWidget(
                bgColor: AppThemeColor.mainColor,
                constraints: const BoxConstraints.expand(width: 100,height: 40),
                text: "交换内容",
                textColor: Colors.white,
                onTap: () => ctl.exchangeClick(),
                alignment: Alignment.center,
              )
            ],
          ),
        ],
      ),
    );
  }

}

